package com.biShe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.biShe.entity.Addresses;
import com.biShe.entity.Category;
import com.biShe.entity.User;
import com.biShe.mapper.AddressesMapper;
import com.biShe.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小刘
 * @since 2023-05-07
 */
@Configuration
@RestController
@RequestMapping("/addresses")
public class AddressesController {

    @Autowired
    private AddressesMapper addressesMapper;

    /**
     * 查找所有地址
     *
     * @return 返回所有地址信息列表
     */
    @ApiOperation(value="获取所有地址信息", notes="返回所有用户地址列表")
    @GetMapping("/list")
    public Result list(){
        List<Addresses> addressesList = addressesMapper.selectList(null);
        return Result.succ(addressesList);
    }

    /**
     * 根据ID查找地址
     *
     * @param id 地址ID
     * @return 返回指定ID的地址信息
     */
    @ApiOperation(value="根据ID获取地址信息", notes="返回指定id的地址信息")
    @ApiImplicitParam(name = "id", value = "地址ID", required = true, dataType = "Int", paramType = "path")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Addresses addresses = addressesMapper.selectById(id);
        if (addresses == null) {
            return Result.fail("该用户不存在");
        }
        return Result.succ(addresses);
    }

    /**
     * 添加新地址
     *
     * @param address 新用户信息
     * @return 返回添加结果
     */
    @ApiOperation(value="添加新地址", notes="添加新地址信息")
    @ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "address")
    @PostMapping("/add")
    public Result add(Addresses address) {
        int rows = addressesMapper.insert(address);
        QueryWrapper<Addresses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accept_name",address.getAcceptName());//这里的column要和数据库保存一致
        queryWrapper.eq("mobile",address.getMobile());
        Addresses address1 = addressesMapper.selectOne(queryWrapper);
        if (rows == 0) {
            return Result.fail("添加新地址失败");
        }
        return Result.succ(address1);
    }

    /**
     * 更新部分地址信息，没收到的信息就不变
     *
     * @param address 地址信息
     * @return 返回更新结果
     */
    @ApiOperation(value="修改用户信息", notes="修改指定用户的信息")
    @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
    @PutMapping("/update")
    public Result update(Addresses address) {
        LambdaUpdateWrapper<Addresses> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Addresses::getId,address.getId());
        updateWrapper.set(Addresses::getAcceptName, address.getAcceptName());
        updateWrapper.set(Addresses::getSex, address.getSex());
        updateWrapper.set(Addresses::getMobile, address.getMobile());
        updateWrapper.set(Addresses::getStreet, address.getStreet());
        updateWrapper.set(Addresses::getDoorNumber, address.getDoorNumber());
        int rows =  addressesMapper.update(null,updateWrapper);
        if (rows == 0) {
            return Result.fail("更新地址信息失败");
        }
        return Result.succ("更新地址信息成功");
    }

    /**
     * 删除地址信息
     *
     * @param id 地址ID
     * @return 返回删除结果
     */
    @ApiOperation(value="删除地址信息", notes="删除指定ID的地址信息")
    @ApiImplicitParam(name = "id", value = "地址ID", required = true, dataType = "Int", paramType = "path")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        int rows = addressesMapper.deleteById(id);
        if (rows == 0) {
            return Result.fail("删除用户信息失败");
        }
        return Result.succ("删除用户信息成功");
    }


    /**
     * PC查找所有地址
     *
     * @return 返回所有地址信息列表
     */
    @ApiOperation(value="获取所有地址信息", notes="返回所有地址列表")
    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page") String page, @PathVariable("size") String size) {
        Page pageHelper = PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(size));
        List<Addresses> categoryList = addressesMapper.selectList(null);
        PageInfo pageInfo = new PageInfo(categoryList);
        return Result.succ(pageInfo);
    }


    /**
     * 根据分类名模糊查询分类信息
     *
     * @param name 分类名
     * @return 返回符合条件的分类信息列表
     */
    @ApiOperation(value="根据分类名模糊查询分类信息", notes="返回符合条件的分类信息列表")
    @ApiImplicitParam(name = "name", value = "分类名", required = true, dataType = "String", paramType = "path")
    @GetMapping("/search/{name}")
    public Result search(@PathVariable("name") String name) {
        QueryWrapper<Addresses> wrapper = new QueryWrapper<>();
        wrapper.like("street", name);
        List<Addresses> categoryList = addressesMapper.selectList(wrapper);
        if (categoryList.isEmpty()) {
            return Result.fail("暂无匹配结果");
        }
        return Result.succ(categoryList);
    }

}

